package com.avic.modules.task.service.impl;


import com.avic.common.service.impl.BaseServiceImpl;
import com.avic.modules.equipment.domain.Equipment;
import com.avic.modules.equipment.service.EquipmentService;
import com.avic.modules.equipment.service.mapper.EquipmentMapper;
import com.avic.modules.task.domain.EquipEhr;
import com.avic.modules.task.domain.EquipMaintenaceStrategy;
import com.avic.modules.task.domain.EquipRul;
import com.avic.modules.task.service.EquipEhrService;
import com.avic.modules.task.service.mapper.EquipEhrMapper;
import com.avic.modules.task.service.mapper.EquipMaintenaceStrategyMapper;
import com.avic.modules.task.service.mapper.EquipRulMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangzikang
 * @date 2020-05-13
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EquipEhrServiceImpl extends BaseServiceImpl<EquipEhrMapper, EquipEhr> implements EquipEhrService {

    // 设备接口
    private final EquipmentMapper equipMapper;
    private final EquipmentService equipService;
    // 设备健康等级
    private final EquipEhrMapper ehrMapper;
    // 剩余使用寿命预测
    private final EquipRulMapper rulMapper;
    // 维护策略
    private final EquipMaintenaceStrategyMapper strategyMapper;

    /**
     * 更新设备健康数据
     */
    @Override
    @Scheduled(cron = "0 0/5 * * * ?")
    public void updateHealth() {
        List<Equipment> equipment = equipMapper.selectList(new QueryWrapper<>());
        for(Equipment equip:equipment){
            if(equip.getWhetherToRead()==1){
                QueryWrapper<EquipEhr> last = new QueryWrapper<>();
                last.eq("equip_id", equip.getId()).orderByDesc("time").last("limit 1");
                EquipEhr equipEhr = ehrMapper.selectOne(last);
                if(equipEhr!=null){
                    equip.setHealthStatus(equipEhr.getEhr());
                }else {
                    equip.setHealthStatus(0);
                }

                QueryWrapper<EquipRul> rul = new QueryWrapper<>();
                rul.eq("equip_id", equip.getId()).orderByDesc("time").last("limit 1");
                EquipRul equipRul = rulMapper.selectOne(rul);
                if(equipRul!=null){
                    equip.setForecast("预测"+ equipRul.getRul() +"天后出现故障");
                }else {
                    equip.setForecast("预测天后出现故障");
                }

                QueryWrapper<EquipMaintenaceStrategy> strategy = new QueryWrapper<>();
                strategy.eq("equip_id", equip.getId()).orderByDesc("time").last("limit 1");
                EquipMaintenaceStrategy equipStrategy = strategyMapper.selectOne(strategy);
                if(equipStrategy!=null){
                    equip.setStrategy("建议"+equipStrategy.getMaintenanceStrategy()+"天后进行维修");
                }else {
                    equip.setStrategy("建议天后进行维修");
                }
            }
        }
        equipService.updateBatchById(equipment);
    }
}
